package com.hks.quart.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author lqzhang
 * @date 2022/4/19
 */
@Data
@ApiModel(value = "HttpResult", description = "统一返回数据结构")
public class HttpResult<T> {

    @ApiModelProperty(value = "返回状态码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    private T data;

    public static <E> HttpResult<E> create(HttpStatus httpStatus) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(httpStatus.getCode());
        httpResult.setMsg(httpStatus.getMessage());
        return httpResult;
    }

    public static <E> HttpResult<E> create(HttpStatus httpStatus, String msg) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(httpStatus.getCode());
        httpResult.setMsg(msg);
        return httpResult;
    }

    public static <E> HttpResult<E> create(HttpStatus httpStatus, E data) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(httpStatus.getCode());
        httpResult.setMsg(httpStatus.getMessage());
        httpResult.setData(data);
        return httpResult;
    }

    public static <E> HttpResult<E> create(HttpStatus httpStatus, String msg, E data) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(httpStatus.getCode());
        httpResult.setMsg(msg);
        httpResult.setData(data);
        return httpResult;
    }

    public static <E> HttpResult<E> create(Integer code, String msg, E data) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(code);
        httpResult.setMsg(msg);
        httpResult.setData(data);
        return httpResult;
    }

    public static <E> HttpResult<E> success() {
        return success(null);
    }

    public static <E> HttpResult<E> success(E data) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(200);
        httpResult.setMsg("操作成功");
        httpResult.setData(data);
        return httpResult;
    }

    public static <E> HttpResult<E> fail() {
        return fail(HttpStatus.FAIL.getMessage());
    }

    public static <E> HttpResult<E> fail(String message) {
        HttpResult<E> httpResult = new HttpResult<>();
        httpResult.setCode(HttpStatus.FAIL.getCode());
        httpResult.setMsg(message);
        return httpResult;
    }
}
